home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_C / PRINTR.C < prev    next >
C/C++ Source or Header  |  1991-01-26  |  3KB  |  134 lines

  1. /*
  2.  * This is a very simple demonstration that shows how to use the Print
  3.  * Manager.  In addition to this source you will need to have the following
  4.  * in your project: MacTraps and ANSI-small.
  5.  * ) 1990 Symantec Corp.  All rights reserved.  Converted to C by Phil Shapiro,
  6.  * original Pascal version by Dave Allcott.
  7.  */
  8. #include <PrintTraps.h>
  9. #include <stdio.h>
  10. #include <pascal.h>
  11.  
  12. #ifndef NIL
  13. #define NIL 0L
  14. #endif 
  15.  
  16. /* Do you want to prompt for the page setup? */
  17. Boolean DoPageSetUp  = FALSE;
  18.  
  19. /* Globals */
  20. FILE *aRandomFile;
  21.  
  22. /* Prototypes */
  23. void Init(void);
  24. Boolean DoneDrawingPagesOfATextFile(FILE *someFile);
  25. void PrintStuff(void);
  26. FILE *GotATextFile(void);
  27.  
  28. void
  29. Init(void)
  30. {
  31.     InitGraf(&thePort);
  32.     InitFonts();
  33.     FlushEvents(everyEvent, 0);
  34.     InitWindows();
  35.     TEInit();
  36.     InitDialogs(0L);
  37.     InitCursor();
  38. }
  39.  
  40. Boolean DoneDrawingPagesOfATextFile(FILE *someFile)
  41. {
  42.     int i;
  43.     Str255 aStringOfText;
  44.  
  45.     TextSize(10);
  46.     TextFont(courier);
  47.     for (i = 1; i <= 50; ++i) {
  48.         if (feof(someFile))
  49.             return TRUE;
  50.         fgets((char *)aStringOfText, 255, someFile);
  51.         CtoPstr((char *)aStringOfText);
  52.         /*
  53.          * Carrige return characters mess up DrawString, so they are removed.
  54.          * Also, tabs are not handled correctly.  They are left as an exercise
  55.          * for the programmer!
  56.          */
  57.         if (aStringOfText[aStringOfText[0]] == '\n')
  58.             aStringOfText[aStringOfText[0]] = ' ';
  59.         MoveTo(10, 14 * i);
  60.         DrawString(aStringOfText);
  61.     }
  62.     return FALSE;
  63. }
  64.  
  65. void PrintStuff(void)
  66. {
  67.     GrafPtr savedPort;
  68.     TPrStatus prStatus;
  69.     TPPrPort printPort;
  70.     THPrint hPrint;
  71.     Boolean ok;
  72.  
  73.     GetPort(&savedPort);
  74.     PrOpen();
  75.     hPrint = (THPrint)NewHandle(sizeof(TPrint));    /* *not* sizeof(THPrint) */
  76.     PrintDefault(hPrint);
  77.     if (DoPageSetUp)
  78.         ok = PrStlDialog(hPrint);
  79.     else
  80.         ok = PrValidate(hPrint);
  81.     ok = PrJobDialog(hPrint);
  82.     if (ok) {
  83.         printPort = PrOpenDoc(hPrint, NIL, NIL);
  84.         SetPort(&printPort->gPort);
  85.         PrOpenPage(printPort, NIL);
  86.         /*
  87.          * Now draw a single page.  You decide how many pages are drawn, and what
  88.          * is drawn in each page.  QuickDraw commands are automatically translated
  89.          * into commands to the printer. You could draw multiple pages like this.
  90.          */
  91.         while (!DoneDrawingPagesOfATextFile(aRandomFile)) {
  92.             PrClosePage(printPort);        /* Close the currently open page. */
  93.             PrOpenPage(printPort, NIL);    /* and open a new one. */
  94.         }
  95.         PrClosePage(printPort);
  96.         PrCloseDoc(printPort);
  97.         /* Print spooled document, if any. */
  98.         if ((**hPrint).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr)
  99.             PrPicFile(hPrint, NIL, NIL, NIL, &prStatus);
  100.     }
  101.     else {
  102.         /* You will want to add error handling here... */
  103.         SysBeep(5);
  104.     }
  105.     PrClose();
  106.     SetPort(savedPort);
  107. }
  108.  
  109. /* Open any text file */
  110. FILE * GotATextFile(void)
  111. {
  112.     SFReply theReply;
  113.     SFTypeList theTypeList = {'TEXT'};
  114.     Point where = {40, 60};
  115.     OSErr PotentialErr;
  116.     FILE *someFile;
  117.  
  118.     SFGetFile(where, "\p", NIL, 1, theTypeList, NIL, &theReply);
  119.     if (theReply.good) {
  120.         PotentialErr  = SetVol(NIL, theReply.vRefNum);
  121.         someFile = fopen(PtoCstr((char *)theReply.fName), "r");
  122.         return someFile;
  123.     }
  124.     return NIL;
  125. }
  126.  
  127. main()
  128. {
  129.     Init();
  130.     if (aRandomFile = GotATextFile()) {
  131.         PrintStuff();
  132.         fclose(aRandomFile);
  133.     }
  134. }